package com.example.chessbattleplatform.exception_advice;


import com.example.chessbattleplatform.Pojo.Exception.BusinessException;
import com.example.chessbattleplatform.Pojo.Result.WebResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常拦截
 * RestControllerAdvice REST 控制器建议
 * ExceptionHandler 异常处理程序
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 自定义异常 拦截
     * BusinessException
     */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public WebResult<?> businessperson(BusinessException e) {
        log.info("业务异常拦截 {}", e.getMessage());
        return new WebResult<>(e.getStatus(), false, e.getMessage(), null);
    }


    /**
     * 未知的运行时异常拦截
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public WebResult<?> notFount(RuntimeException e) {

        log.info("____________________未知的运行时异常拦截____________________ \r\n {}", e.toString());
        log.info("========================================================");

        log.error("返回异常的详细描述信息 {}", e.getMessage());
        log.error("异常类 {}", e.getClass());

        log.error("获取堆栈跟踪的数组，可自定义处理 \r\n");
        e.printStackTrace();

        log.info("________________________________________________________");
        return WebResult.error(e.getMessage());
    }


}
